﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Infrastructure;
using Enterprise.Model.OfficeDocument;
using Enterprise.Service.OfficeDocument;
using Enterprise.Model.WorkFlow;
using Enterprise.Service.WorkFlow;
namespace Enterprise.UI.Web.Modules.OfficeDocument
{
    public partial class OfficeDocumentCheck : Enterprise.Service.BasePage
    {
        string ODID = (string)Utility.sink("ODID", Utility.MethodType.Get, 0, 0, Utility.DataType.Str);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();
            }
        }

        private void OnStart()
        {
            OfficeDocService odService = new OfficeDocService();
            var query = odService.OfficeDocumentDisp(ODID);
            if (query != null)
            {
                lb_Dept.Text = Enterprise.Service.Department.DepartmentService.GetDepartMentName(query.ODDEPTID);
                lb_User.Text = Enterprise.Service.User.UserService.GetUserName(query.ODUSERID);
                lb_FromTime.Text = query.ODFROMTIME;
                lb_FromUser.Text = query.ODFROMUSER;
                lb_From.Text = query.ODFROM;
                lb_Code.Text = query.ODCODE;
                lb_Name.Text = query.ODCNAME;
                lb_RName.Text = query.ODRNAME;
                lb_Content.Text = query.ODCREMARK;
                lb_RContent.Text = query.ODRREMARK;
                lb_Files.Text = query.ODFILES;
                lb_Time.Text = query.ODCREATETIME.ToString();
                if (query.ODTYPEID == 0)
                {
                    ulTime.Visible = false;
                    ulFrom.Visible = false;
                }
                WorkFlowProcessService wfpService = new WorkFlowProcessService();
                GridView1.DataSource = wfpService.WorkFlowProcessListByWorkFlow(query.ODID);
                GridView1.DataBind();
            }
        }

        protected void BtnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                OfficeDocService odService = new OfficeDocService();
                var q = odService.OfficeDocumentDisp(ODID);
                if (q != null)
                {
                    WorkFlowProcessService wfpService = new WorkFlowProcessService();
                    var query = wfpService.WorkFlowProcessList().Where(p => p.WFID == ODID && p.PUSERID == Utility.Get_UserId && p.PISCHECK == 0).FirstOrDefault();
                    if (query != null)
                    {
                        query.DB_Option_Action = "Update";
                        query.PISCHECK = 1;
                        query.PRESULT = 1;
                        query.PREMARK = (string)Utility.sink(tb_CheckRemark.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str); //(string)General.sink(P_CheckRemark.UniqueID, General.MethodType.Post, 0, 0, General.DataType.Str);
                        query.PTIME = DateTime.Now;
                        wfpService.WorkFlowProcessExecute(query);
                        if (query.PISCHECK == 1)
                        {
                            odService.Notice(q, query.PUSERORDERBY);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "OfficeDocumentIndex.aspx");
        }

        protected void BtnUnSave_OnClick(object sender, EventArgs e)
        {
            string _note = "操作成功";
            try
            {
                OfficeDocService odService = new OfficeDocService();
                var q = odService.OfficeDocumentDisp(ODID);
                if (q != null)
                {
                    WorkFlowProcessService wfpService = new WorkFlowProcessService();
                    var query = wfpService.WorkFlowProcessList().Where(p => p.WFID == ODID && p.PUSERID == Utility.Get_UserId && p.PISCHECK == 0).FirstOrDefault();
                    if (query != null)
                    {
                        query.DB_Option_Action = "Update";
                        query.PISCHECK = 1;
                        query.PRESULT = 0;
                        query.PREMARK = (string)Utility.sink(tb_CheckRemark.UniqueID, Utility.MethodType.Post, 0, 0, Utility.DataType.Str); //(string)General.sink(P_CheckRemark.UniqueID, General.MethodType.Post, 0, 0, General.DataType.Str);
                        query.PTIME = DateTime.Now;
                        wfpService.WorkFlowProcessExecute(query);
                        if (query.PISCHECK == 1)
                        {
                            //审批未通过，提醒申请人进行重新提交
                            q.ODSTATE = 2;
                            string cmd = "Update OFFICE_DOCUMENT set ODSTATE=" + q.ODSTATE + " where ODID='" + q.ODID + "'";
                            odService.OfficeDocumentExecuteBySql(cmd, 1);
                            string sTitle = string.Format("审批流程[您申请的公文({0})]", q.ODCNAME);
                            string sContent = sTitle + string.Format(",未通过审批流程，请查看审批意见进行下一步操作!");
                            Enterprise.Service.Rtx.RtxService.SendRtxMessageServices(q.ODUSERID, sTitle, sContent);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _note = ex.Message;
            }
            Utility.ShowMsg(this.Page, "OK", this.Tran(_note), "OfficeDocumentIndex.aspx");
        }

    }
}